package web.webSocket.hndler;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPServer implements Runnable {
	private DatagramSocket serverSocket ;
	public UDPServer(){
		try {
			serverSocket = new DatagramSocket(8077);
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		while(true){
			byte[] receiveData = new byte[2000];          
			DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
			try {
				serverSocket.receive(receivePacket);
				String sentence = new String( receivePacket.getData(),"UTF-8");
				String[] bags = sentence.split("H");
				for(int i=0,l=bags.length;i<l;i++){
					String _bag = bags[i];
					if(_bag.length() == 0) continue;
					if(_bag.split("T").length == 1) continue;
					String bag = _bag.split("T")[0];
					String[] datas = bag.split("&");
					if(datas.length !=6) continue;
					SystemWebSocketHandler.sendByUnicode(datas[0], bag);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
